package com.example2016.cjj.rotationcarddemo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FrameLayout contentFl;
    private FrameLayout backCardFl;
    private FrameLayout frontCardFl;

    private AnimatorSet outAnimatorSet;
    private AnimatorSet inAnimatorSet;

    private boolean isFrontFlag=true;//是否正面标识

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentFl = (FrameLayout) findViewById(R.id.fl_content);
        contentFl.setOnClickListener(this);

        backCardFl = (FrameLayout) findViewById(R.id.fl_backcard);
        frontCardFl = (FrameLayout) findViewById(R.id.fl_frontcard);



        initAnimator();

        setCameraDistance();

    }

    //初始化动画
    private void initAnimator() {
        //旋转180°, 当旋转一半时, 卡片消失
        outAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.card_out);
        outAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                //动画开始后，不可点击
                contentFl.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        //在开始时是隐藏, 逆向旋转, 当旋转一半时, 显示卡片
        inAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.card_in);
        inAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束后，可点击
                contentFl.setClickable(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });


    }

    //设置镜头距离，其实就是设置z轴
    private void  setCameraDistance(){
        int distance = 19200;
        float scale =  getResources().getDisplayMetrics().density ;
        frontCardFl.setCameraDistance(scale * distance);
        backCardFl.setCameraDistance(scale * distance );
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_content:
                if(isFrontFlag){
                    //正面朝上
                    outAnimatorSet.setTarget(frontCardFl);
                    outAnimatorSet.start();

                    inAnimatorSet.setTarget(backCardFl);
                    inAnimatorSet.start();

                    isFrontFlag = false;
                }else {
                    //反面朝上
                    outAnimatorSet.setTarget(backCardFl);
                    outAnimatorSet.start();

                    inAnimatorSet.setTarget(frontCardFl);
                    inAnimatorSet.start();

                    isFrontFlag = true;
                }



                break;
        }
    }
}
